<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<div class="bjui-pageHeader" style="background-color:#fefefe; border-bottom:none;">
    <form data-toggle="ajaxsearch" id="user_form">
        <fieldset>
            <legend style="font-weight:normal;">条件搜索：</legend>
            <div style="margin:0; padding:1px 15px 15px;">
                <label class="row-label">用户名：</label>
                <input id="userName" type="text" name="userName" class="form-control" value="${userName}"
                       title="用户名" data-toggle="tags" data-url="/admin/user/autoTags" data-width="130">

                <%--<label class="row-label">&nbsp;标号：</label>--%>
                <%--<input type="text" name="loanNumber" class="form-control" size="15" title="标的编号">--%>

                <%--<label class="row-label">&nbsp;流水号：</label>--%>
                <%--<input type="text" name="orderNo" class="form-control" size="15" title="流水号">--%>

                <label class="row-label">&nbsp;交易时间：</label>
                <input type="text" name="from" data-toggle="datepicker" data-nobtn="true" size="11">
                <label class="row-label">-</label>
                <input type="text" name="to" data-toggle="datepicker" data-nobtn="true" size="11">
                <label class="row-label"></label>
                <div class="btn-group">
                    <button id="submit" type="submit" class="btn-green" data-icon="search" onclick="userAjaxSearch()">
                        搜索
                    </button>
                    <button type="reset" class="btn-orange" data-icon="times" onclick="$('#userName').attr('value','')">重置</button>
                </div>
            </div>
        </fieldset>
    </form>
    <div style="margin:0; padding:5px 20px 15px;">
        <span>充值总额：</span>
        <span id="recharge_total">0.00</span>&nbsp;

        <span>提现总额：</span>
        <span id="withdraw_total">0.00</span>&nbsp;

        <span>投资总额：</span>
        <span id="purchaseLoan_total">0.00</span>&nbsp;

        <span>累计收益：</span>
        <span id="userJournal_userProfit">0.00</span>&nbsp;
    </div>
    <div style="margin:0; padding:5px 20px 15px;">
        <span>累计预收益：</span>
        <span id="userJournal_totalCollect">0.00</span>&nbsp;

        <span>收款总额：</span>
        <span id="collection_total">0.00</span>&nbsp;

        <span>借款总额：</span>
        <span id="loan_total">0.00</span>&nbsp;

        <span>还款总额：</span>
        <span id="repayment_total">0.00</span>&nbsp;
    </div>
</div>
<div class="bjui-pageContent">
    <table id="userJournal_recharge_table" class="table table-bordered"></table>
    <table id="userJournal_withdraw_table" class="table table-bordered"></table>
    <table id="userJournal_purchaseLoan_table" class="table table-bordered"></table>
    <table id="userJournal_collection_table" class="table table-bordered"></table>
    <table id="userJournal_repayment_table" class="table table-bordered"></table>
    <table id="userJournal_loan_table" class="table table-bordered"></table>
</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/toolDate.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/loan/loanDisplay.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/capital/capital.js"></script>
<script type="text/javascript">
    var userJournal_recharge_table, userJournal_withdraw_table, userJournal_purchaseLoan_table,
        userJournal_collection_table, userJournal_repayment_table, userJournal_loan_table;
    $(function () {
        userJournal_recharge_table = $("#userJournal_recharge_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            showToolbar: true,
            filterThead: false,
            gridTitle: '充值记录',
            showTfoot: true,
            delPK: 'id',
            dataUrl: '/admin/capital/flow?userJournal=true&type=0&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            postData: {total: "userJournal_recharge"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="userExport();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                render: showUser,
                label: '用户名',
                align: 'center'
            }, {
                name: 'amount',
                label: '充值金额',
                align: 'center',
                total: 'userJournal_recharge'
            }, {
                name: 'date',
                label: '交易时间',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 19) : value
                }
            }, {
                name: 'status',
                label: '交易状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center',
                width: 250
            }],
            paging: {pageSize: 3, pageIndex: 1, selectPageSize: '3,5,10'},
            linenumberAll: true
        });

        userJournal_withdraw_table = $("#userJournal_withdraw_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            gridTitle: '提现记录',
            showToolbar: true,
            filterThead: false,
            showTfoot: true,
            delPK: 'id',
            dataUrl: '/admin/capital/flow?userJournal=true&type=1&_=' + new Date().getTime() + 2,
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            postData: {total: "userJournal_withdraw"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="userExport();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                render: showUser,
                label: '用户名',
                align: 'center'
            }, {
                name: 'amount',
                label: '提现金额',
                align: 'center',
                total: 'userJournal_withdraw'
            }, {
                name: 'date',
                label: '提现时间',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 19) : value
                }
            }, {
                name: 'status',
                label: '交易状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center'
            }],
            paging: {pageSize: 3, pageIndex: 1, selectPageSize: '3,5,10'},
            linenumberAll: true
        });

        userJournal_purchaseLoan_table = $("#userJournal_purchaseLoan_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            gridTitle: '投资记录',
            showToolbar: true,
            filterThead: false,
            delPK: 'id',
            dataUrl: '/admin/capital/loan?userJournal=true&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            showTfoot: true,
            postData: {total: "userJournal_purchaseLoan"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="userExportLoan();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                render: showUser,
                label: '用户名',
                align: 'center'
            }, {
                name: 'payPlatformID',
                label: '双乾标识',
                align: 'center'
            }, {
                label: '标的编号',
                align: 'center',
                render: function (value, data) {
                    if (value) {
                        return '<a href="javascript:loanInfo(\'' + data.loanId + '\')">' + data.loanNumber + '</a>';
                    }
                    return '';
                }
            }, {
                name: 'amount',
                label: '金额',
                align: 'center',
                total: 'userJournal_purchaseLoan'
            }, {
                name: 'date',
                label: '购买日期',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 19) : value
                }
            }, {
                name: 'status',
                label: '交易状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center'
            }],
            paging: {pageSize: 3, pageIndex: 1, selectPageSize: '3,5,10'},
            linenumberAll: true
        });
        userJournal_collection_table = $("#userJournal_collection_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            gridTitle: '收款记录',
            showToolbar: true,
            filterThead: false,
            showTfoot: true,
            delPK: 'id',
            dataUrl: '/admin/capital/flow?userJournal=true&type=5&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            postData: {total: "userJournal_collection"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="userExport();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                render: showOutUser,
                label: '收款人',
                align: 'center'
            }, {
                name: 'amount',
                label: '收款金额',
                align: 'center',
                total: 'userJournal_collection'
            }, {
                name: 'loanNumber',
                label: '标号',
                align: 'center',
                render: function (value, data) {
                    if (value) {
                        return '<a href="javascript:loanInfo(\'' + data.loanId + '\')">' + data.loanNumber + '</a>';
                    }
                    return '';
                }
            }, {
                name: 'date',
                label: '交易时间',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 19) : value
                }
            }, {
                name: 'status',
                label: '交易状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center'
            }],
            paging: {pageSize: 3, pageIndex: 1, selectPageSize: '3,5,10'},
            linenumberAll: true
        });
        userJournal_repayment_table = $("#userJournal_repayment_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            gridTitle: '还款记录',
            showToolbar: true,
            filterThead: false,
            delPK: 'id',
            showTfoot: true,
            dataUrl: '/admin/capital/flow?userJournal=true&type=2&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            postData: {total: "userJournal_repayment"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="userExport();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                render: showOutUser,
                label: '还款人',
                align: 'center',
                thidden: true,
                total: 'totalUserCollect'
            }, {
                name: 'amount',
                label: '还款金额',
                align: 'center',
                total: 'userJournal_repayment'
            }, {
                name: 'loanNumber',
                label: '标号',
                align: 'center',
                render: function (value, data) {
                    if (value) {
                        return '<a href="javascript:loanInfo(\'' + data.loanId + '\')">' + data.loanNumber + '</a>';
                    }
                    return '';
                }
            }, {
                name: 'date',
                label: '交易时间',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 19) : value
                }
            }, {
                name: 'status',
                label: '交易状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center',
                total:'totalUserProfit',
                thidden : true

            }],
            paging: {pageSize: 3, pageIndex: 1, selectPageSize: '3,5,10'},
            linenumberAll: true
        });
        userJournal_loan_table = $("#userJournal_loan_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            gridTitle: '借款记录',
            showToolbar: true,
            filterThead: false,
            delPK: 'id',
            showTfoot: true,
            dataUrl: '/admin/loan/loan/ajax/all?userJournal=true&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            columns: [
                {
                    label: '标的编号',
                    align: 'center',
                    render: function (value, data) {
                        if (data.loan === undefined) {
                            return '';
                        }
                        return '<a href="javascript:loanInfo(\'' + data.loan.loanId + '\')">' + data.loan.loanNumber + '</a>';
                    }
                },
                {
                    label: '融资金额',
                    align: 'center',
                    render: function (value, data) {
                        if (data.loan === undefined) {
                            return '';
                        }
                        var loanAmountD = data.loan.loanAmountD;
                        return loanAmountD + "元";
                    },
                    total: 'all_loanAmountD'
                },
                {
                    label: '年利率',
                    align: 'center',
                    render: function (value, data) {
                        if (data.loan === undefined) {
                            return '';
                        }
                        return data.loan.rateD + "%";
                    }
                },
                {
                    label: '借款期限',
                    align: 'center',
                    render: function (value, data) {
                        if (data.loan === undefined) {
                            return '';
                        }
                        return data.loan.loanTime + "天";
                    }
                },
                {
                    label: '状态',
                    align: 'center',
                    render: function (value, data) {
                        if (data.loan === undefined) {
                            return '';
                        }
                        return data.loan.loanStatusStr;
                    }
                },
                {
                    label: '发布时间',
                    align: 'center',
                    render: function (value, data) {
                        if (data.loan === undefined) {
                            return '';
                        }
                        var date = new Date(data.loan.addDate);
                        return date.Format("yyyy年MM月dd日");
                    }
                }
            ],
            postData: {total: "userJournal_loan"},
            paging: {pageSize: 3, pageIndex: 1, selectPageSize: '3,5,10'},
            linenumberAll: true
        });

        function showUser(value, data) {
            return '<a href="javascript:;" onclick="openDialog(\'' + data.userId + "','" + data.userName + '\')">' + data.userName + '</a>';
        }

        function showOutUser(value, data) {
            return '<a href="javascript:;" onclick="openDialog(\'' + data.inUserId + "','" + data.inUserName + '\')">' + data.inUserName + '</a>';
        }

        var userName = $('#userName').val();
        if (userName) {
            setTimeout("clickBt()", 1200)
        }

    });

    function clickBt() {
        $('#submit').click();
    }

    function openDialog(id, userName) {
        BJUI.dialog({
            id: 'userInfo_' + id,
            url: '/admin/user/userInfo?id=' + id,
            title: '' + userName + '用户信息',
            width: 1200,
            height: 1200 * 0.618
        });
    }

    function userExport() {
        var userCollection = $(userJournal_collection_table).data('selectedDatas'),
            userRepayment = $(userJournal_repayment_table).data('selectedDatas'),
            userRecharge = $(userJournal_recharge_table).data('selectedDatas'),
            userWithdraw = $(userJournal_withdraw_table).data('selectedDatas');

        if ((userCollection && userCollection.length !== 0)
            || (userRepayment && userRepayment.length !== 0)
            || (userRecharge && userRecharge.length !== 0)
            || (userWithdraw && userWithdraw.length !== 0)) {
            var ids = [], flows = $.extend(userCollection, userRepayment, userRecharge, userWithdraw);
            $.each(flows, function (index, value, array) {
                ids.push(value.id);
            });
            BJUI.ajax('ajaxdownload', {
                url: '/admin/capital/exportFlow',
                data: {ids: ids.join(",")}
            });
        } else
            BJUI.alertmsg('info', BJUI.getRegional('datagrid.selectMsg'))
    }

    function userExportLoan() {
        var loans = $(userJournal_purchaseLoan_table).data('selectedDatas');
        if (loans && loans.length !== 0) {
            var ids = [];
            loans.forEach(function (e) {
                ids.push(e.id);
            });
            BJUI.ajax('ajaxdownload', {
                url: '/admin/capital/exportLoan',
                data: {ids: ids.join(",")}
            });
        } else
            BJUI.alertmsg('info', BJUI.getRegional('datagrid.selectMsg'))
    }

    function userAjaxSearch() {
        var data = $('#user_form').serializeJson();
        $(userJournal_recharge_table).datagrid('filter', data);
        $(userJournal_withdraw_table).datagrid('filter', data);
        $(userJournal_purchaseLoan_table).datagrid('filter', data);
        $(userJournal_collection_table).datagrid('filter', data);
        $(userJournal_repayment_table).datagrid('filter', {
            userName: data.userName,
//            inUserName: data.inUserName,
//            orderNo: data.orderNo,
            'fromTo.from': data.from,
            'fromTo.to': data.to
        });
        $(userJournal_loan_table).datagrid('filter', {
            userName: data.userName,
//            loanNumber: data.loanNumber,
            'dateFromTo.from': data.from,
            'dateFromTo.to': data.to
        });
    }

    function callbackset() {
        $('#recharge_total').html($('#userJournal_recharge').html());
        $('#withdraw_total').html($('#userJournal_withdraw').html());
        $('#purchaseLoan_total').html($('#userJournal_purchaseLoan').html());
        $('#collection_total').html($('#userJournal_collection').html());
        $('#loan_total').html($('#all_loanAmountD').html());
        $('#repayment_total').html($('#userJournal_repayment').html());
        $('#userJournal_totalCollect').html($('#totalUserCollect').html());
        $('#userJournal_userProfit').html($('#totalUserProfit').html());
    }

    $('#userName').on('aftercreated.bjui.tags', function(e, data) {
        userAjaxSearch();
    });
</script>